<?php

namespace app\admin\controller;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;
use dh2y\qrcode\QRcode;

/**
 * @ControllerAnnotation(title="试玩列表")
 */
class Demo extends AdminController
{

    use \app\admin\traits\Curd;
    protected $sort = [
        'sort'   => 'asc',
    ];
    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\Demo();
        
    }

     /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            foreach ($list as $k => $v){
                //分类
                $classification = Db::name("classification")->where("id",$v["class_id"])->find();
                if(!empty($classification)){
                    $v["class_name"] = $classification["title"];
                }else{
                    $v["class_name"] = "";
                }
                $list[$k] = $v;
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }else{
            $classification = Db::name("classification")->where("status",0)->select();
            $this->assign("classification",$classification);
            return $this->fetch();
        }
      
    }


    /**
    * @NodeAnotation(title="添加")
    */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['demo_name'])){
                $this->error('试玩APP名称不能为空');
            }
            if(empty($post['class_id'])){
                $this->error('分类不能为空');
            }
            $data = [
                'class_id'=>$post['class_id'],
                'demo_name'=>$post['demo_name'],
                'demo_img'=>$post['demo_img'],
                'price'=>$post['price'],
                'jump_url'=>$post['jump_url'],
                'create_time'=>time(),
                'update_time'=>time()
            ];
            try {
                Db::name('demo')->insert($data);
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $this->success('保存成功');
        }else{
            $classification = Db::name("classification")->where("status",0)->select();
            $this->assign("classification",$classification);
            return $this->fetch();
        }
    }

    
     /**
    * @NodeAnotation(title="编辑")
    */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['demo_name'])){
                $this->error('试玩APP名称不能为空');
            } 
            if(empty($post['class_id'])){
                $this->error('分类不能为空');
            }
            $data = [
                'class_id'=>$post['class_id'],
                'demo_name'=>$post['demo_name'],
                'demo_img'=>$post['demo_img'],
                'price'=>$post['price'],
                'jump_url'=>$post['jump_url'],
                'update_time'=>time()
            ];
            try {
               $this->model->where('id',$post['id'])->update($data);
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $this->success('保存成功') ;
        }else{
            $post = $this->request->param(); 
            $row = $this->model->where('id',$post['id'])->find();
            // $row['desc'] = html_entity_decode($row['desc']);
            // $row['desc'] = str_replace(array("\r", "\n\n", "\s", "\t", "\n"), '<br/>', $row['desc']);
            $classification = Db::name("classification")->where("status",0)->select();
            $this->assign("classification",$classification);
            $this->assign('row', $row); 
            return $this->fetch();
        }
       
    }

    
   
    
    
}